import java.util.*;
public class Solution {
    /*递归*/
    private List<Integer> path = new ArrayList<>();
    private List<List<Integer>> res = new ArrayList<>();
    private int sum = 0;
    private int targetSum;



    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        this.targetSum = targetSum;
        dfs(root);
        return res;
    }
    public void dfs(TreeNode node) {
        if (node == null) {
            return;
        }
        path.add(node.val);
        sum += node.val;
        if (node.left == null && node.right == null && sum == targetSum) {
            res.add(new ArrayList<>(path)); // 这一这里数组的话用copyOf（Range）

        }
        dfs(node.left);
        dfs(node.right);
        sum -= node.val;
        path.remove(path.size() - 1);

    }

    /*迭代广度待补充*/
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}